package com.nhjf.admin.common.util;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.nhjf.common.Constants;

public class StringUtil {
	
	
    /****
	 * 检测字符串不为空，并且不等于''
	 * 返回true表示不为空，返回false表示为空
	 * @return
	 */
	public  static boolean isNotNull(String str)
	{
	    if(str!=null && !str.trim().equals("")&& !"null".equals(str.trim().toLowerCase()))
	    {
	    	return true;
	    }
	    return false;
	}
	
	
	 /**
	  * 检测字符是否是数字
	 * @param str
	 * @return 是纯数字返回true,否则返回false;
	 */
	public static boolean isNumeric(String str){
		  for (int i = str.length();--i>=0;){
		   if (!Character.isDigit(str.charAt(i))){
		    return false;
		   }
		  }
		  return true;
	}
	
	 /****
	 * 检测集合 List 是否为空，size 是否大于 0
	 * 返回值：true 表示不为空且size 大于 0
	 * @return
	 */
	public  static boolean isNotNull(List list)
	{
	    if(list != null && list.size()>0)
	    {
	    	return true;
	    }
	    return false;
	}

	
	/**
	 * 判断2个字符串是否相等
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean equal(String a,String b){
		boolean result = false;
		if(null==a||""==a){
			if(null==b||""==b){
				result=true;
			}
		}else{
			if(null!=b&&a.trim().equals(b.trim())){
				result=true; 
			}
		}
		return result;
	}
	
	/**
	 * 转码
	 * @return
	 */
    public static final String changeCode(String str, int unicodeFlag) {
        try {
            switch (unicodeFlag) {
            case 0:
                return str;
            case 1:
                return new String(str.getBytes("ISO-8859-1"), "gb2312");
            case 2:
                return new String(str.getBytes("gb2312"), "ISO-8859-1");
            case 3:
                return new String(str.getBytes("UTF-8"), "gb2312");
            case 4:
                return new String(str.getBytes("gb2312"), "UTF-8");
            case 5:
                return new String(str.getBytes("GBK"), "ISO-8859-1");
            case 6:
                return new String(str.getBytes("ISO-8859-1"), "GBK");                    
            default:
                return str;
            }
        } catch (Exception ex) {
            return "";
        }
    }
    
	/**
	 * 转码2
	 * @return
	 */
    public static String getString(String s, String srcEncoding, String destEncoding) {
        try {
            return new String(s.getBytes(srcEncoding), destEncoding);
        } catch (UnsupportedEncodingException e) {
            return s;
        }
    }
    
	/**
	 * 前面补零
	 * @param a
	 * @param b
	 * @return
	 */
	public static String front(String str,int lenght){
		
		for(;str.length()<lenght;)
		{
			str="0"+str;
		}
		return str;
	}
	
	/**
	 * 后面补零
	 * @param a
	 * @param b
	 * @return
	 */
	public static String back(String str,int lenght){
		
		for(;str.length()<lenght;)
		{
			str=str+"0";
		}
		return str;
	}
	
	

	
	public static String nullTo(Object o){
		if(o==null)
			return "";
		return o.toString().trim();
	}

	/**
	 * 获取该天得最后时刻
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date getEndDate(Date date) throws ParseException{
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		String dd;
		try {
			dd = myFormatter.format(date);
		} catch (Exception e) {
			dd = myFormatter.format(new Date());
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return format.parse(dd + " 23:59:59");
	}
	
	/**
	 * 获取该天开始时间
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date getStartDate(Date date) throws ParseException{
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		String dd;
		try {
			dd = myFormatter.format(date);
		} catch (Exception e) {
			dd = myFormatter.format(new Date());
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return format.parse(dd + " 00:00:00");
	}
	
	/**
	 * 
	 * @param sku  SKU编号
	 * @param sequence  序列  1.商品主键 2.颜色 3.尺寸
	 * @return
	 */
	public static Integer getIdForSku(String sku ,int sequence) {
	     String tmp=""; 
		 if(sku!=null){
		     if(sequence==1){
		    	 tmp= sku.substring(2,10);
		     }else if(sequence==2){
		    	 tmp= sku.substring(10,15);
		     }else if(sequence==3){
		    	 tmp= sku.substring(15,20);
		     }
		 }
	     if(!"".equals(toRemoveBeforeChar(tmp,"0"))){
	    	 return Integer.valueOf(toRemoveBeforeChar(tmp,"0"));
	     }else{
	    	 return 0;
	     }
		
		 
	}
	
	/***
	 * 移除某字符前的字符
	 * @param str  原字符串  "00010034"
	 * @param character  字符  "0"
	 * @return 移除字符后的字符串  "10034"
	 */
	public static String toRemoveBeforeChar(String str ,String character){
		String tmp ="" ;
		if(str!=null){
			for(int i=0;i<str.length();i++){
				   if(!character.equals(String.valueOf(str.charAt(i)))){
				    	tmp  = str.substring(i, str.length());
				    	break;
				    } 
			}
		}
		return tmp ;
	}
	                                
	
	public static String getOrderType(String OrderNo){
	    String type ="";
	    if(OrderNo!=null){
	    	if(OrderNo.startsWith("1")){  //普通订单
	    		type = "1" ;
	    	}else if(OrderNo.startsWith("2")){  //团购订单
	    		type = "2" ;
	    	}else if(OrderNo.startsWith("3")){  //秒杀订单
	    		type = "3" ;
	    	}else if(OrderNo.startsWith("4")){   //换货订单类型
	    		type = "4" ;
	    	}
	    }
		return   type ;
	}

	public static String getSign(Map<String, String> params,String privateKey){
		StringBuffer content = new StringBuffer();
		List<String> keys = new ArrayList<String>(params.keySet());
		Collections.sort(keys);
		for (int i = 0; i < keys.size(); i++) {
			String key = (String) keys.get(i);
			String value = params.get(key);
			if(Constants.KEY_SIGN.equalsIgnoreCase(key)){
				continue;
			}
			content.append(value);
		}
		try {
			return MD5.MD5Encode(URLEncoder.encode(content.toString(),"UTF-8").toLowerCase())+privateKey;
		} catch (UnsupportedEncodingException e) {
			LogMessage.logwrite(StringUtil.class, e.getMessage());
			return null;
		}
	}
	

	
}
